﻿@using Microsoft.AspNetCore.Components.Web.Virtualization
@typeparam TNode
@inherits BaseComponent
<CascadingValue Value="@this" IsFixed>
    <div class="@ClassNames" style="@StyleNames" @attributes="@Attributes">
        @if ( NodeStates is not null )
        {
            @if ( Virtualize )
            {
                <Virtualize TItem="TreeViewNodeState<TNode>" Context="node" Items="@NodeStates.ToList()">
                    @nodeFragment( node )
                </Virtualize>
            }
            else
            {
                @foreach ( var nodeState in NodeStates )
                {
                    @nodeFragment( nodeState )
                }
            }
        }
    </div>
</CascadingValue>

@code {
    protected RenderFragment<TreeViewNodeState<TNode>> nodeFragment => nodeState => __builder =>
    {
        <div @key="@nodeState.Key" @oncontextmenu="@((eventArgs)=>OnContextMenuHandler(nodeState, eventArgs))" @oncontextmenu:stopPropagation @oncontextmenu:preventDefault="@ContextMenuPreventDefault">
            @if ( nodeState.HasChildren )
            {
                <span class="b-tree-view-node-icon" @onclick="@(()=>ToggleNode(nodeState))">
                    @if ( nodeState.Expanded )
                    {
                        <Icon Name="@CollapseIconName" IconStyle="@CollapseIconStyle" IconSize="@CollapseIconSize" />
                    }
                    else
                    {
                        <Icon Name="@ExpandIconName" IconStyle="@ExpandIconStyle" IconSize="@ExpandIconSize" />
                    }
                </span>
            }

            <_TreeViewNodeContent TNode="TNode" NodeState="@nodeState"
                                  NodeStyling="@NodeStyling"
                                  SelectionMode="@SelectionMode"
                                  SelectedNodeStyling="@SelectedNodeStyling"
                                  DisabledNodeStyling="@DisabledNodeStyling">
                @NodeContent( nodeState.Node )
            </_TreeViewNodeContent>

            @if ( nodeState.Expanded && nodeState.HasChildren )
            {
                <_TreeViewNode @ref="@nodeState.ViewRef"
                               NodeStates="@nodeState.Children"
                               NodeContent="@NodeContent"
                               GetChildNodes="@GetChildNodes"
                               GetChildNodesAsync="@GetChildNodesAsync"
                               ExpandedNodes="@ExpandedNodes"
                               ExpandedNodesChanged="@ExpandedNodesChanged"
                               Expanded="@nodeState.Expanded"
                               AutoExpandAll="@AutoExpandAll"
                               HasChildNodes="@HasChildNodes"
                               IsDisabled="@IsDisabled"
                               HasChildNodesAsync="@HasChildNodesAsync"
                               NodeStyling="@NodeStyling"
                               SelectedNodeStyling="@SelectedNodeStyling"
                               DisabledNodeStyling="@DisabledNodeStyling"
                               ExpandIconName="@ExpandIconName"
                               ExpandIconStyle="@ExpandIconStyle"
                               CollapseIconName="@CollapseIconName"
                               CollapseIconStyle="@CollapseIconStyle"
                               SelectionMode="@SelectionMode"
                               ContextMenu="@ContextMenu"
                               ContextMenuPreventDefault="@ContextMenuPreventDefault"
                               Virtualize="@Virtualize" />
            }
        </div>
    };
}